package com.example.wac.mail;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author wuancheng
 * @description
 * @date 2021-08-11 10:55:00
 */
@Slf4j
@Component
public class SendMail {
    @Autowired
    private JavaMailSender mailSender;

    @Resource(name = "threadPoolTaskExecutor")
    ThreadPoolTaskExecutor executor;

    public void commonEmail(ToEmail toEmail) {
        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom(toEmail.getFrom());
        //谁要接收
        message.setTo(toEmail.getTos());
        //邮件标题
        message.setSubject(toEmail.getSubject());
        //邮件内容
        message.setText(toEmail.getContent());

        mailSender.send(message);
    }

    /**
     * 通过execute异步发送邮件
     *
     * @param toEmail
     */
    public void asyncCommonEmail(ToEmail toEmail){
        executor.execute(() -> {
            //创建简单邮件消息
            SimpleMailMessage message = new SimpleMailMessage();
            //谁发的
            message.setFrom(toEmail.getFrom());
            //谁要接收
            message.setTo(toEmail.getTos());
            //邮件标题
            message.setSubject(toEmail.getSubject());
            //邮件内容
            message.setText(toEmail.getContent());
            mailSender.send(message);
        });

//        /**
//         * runnable方法没有返回值
//         */
//        Future<?> submit = executor.submit(() -> {
//            //创建简单邮件消息
//            SimpleMailMessage message = new SimpleMailMessage();
//            //谁发的
//            message.setFrom(toEmail.getFrom());
//            //谁要接收
//            message.setTo(toEmail.getTos());
//            //邮件标题
//            message.setSubject(toEmail.getSubject());
//            //邮件内容
//            message.setText(toEmail.getContent());
//            mailSender.send(message);
//        });
//
//        try {
//            Object o = submit.get();
//            System.out.println(o);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//
//        /**
//         * callable方法有返回值
//         */
//        Future<Object> future = executor.submit(new Callable<Object>() {
//            @Override
//            public Object call() throws Exception {
//                //创建简单邮件消息
//                SimpleMailMessage message = new SimpleMailMessage();
//                //谁发的
//                message.setFrom(toEmail.getFrom());
//                //谁要接收
//                message.setTo(toEmail.getTos());
//                //邮件标题
//                message.setSubject(toEmail.getSubject());
//                //邮件内容
//                message.setText(toEmail.getContent());
//                mailSender.send(message);
//
//                return "OK";
//            }
//        });
//        try {
//            Object o = future.get();
//            System.out.println(o);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }

    }

    /**
     * 通过@Async异步发送邮件
     *
     * @param toEmail
     */
    @Async("threadPoolTaskExecutor")
    public void asyncCommonEmailByAnnotation(ToEmail toEmail){
            //创建简单邮件消息
            SimpleMailMessage message = new SimpleMailMessage();
            //谁发的
            message.setFrom(toEmail.getFrom());
            //谁要接收
            message.setTo(toEmail.getTos());
            //邮件标题
            message.setSubject(toEmail.getSubject());
            //邮件内容
            message.setText(toEmail.getContent());
            mailSender.send(message);
    }

}
